package org.feiyu.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by awei on 2017/7/29.
 */
public class HttpClientUtils {
    public static void main(String[] args) throws IOException {
        String url = "http://ossteacher.oss-cn-beijing.aliyuncs.com/import_study_circle?Expires=1816320212&OSSAccessKeyId=LTAIu6xCmzZycioU&Signature=M%2B9Ni8YcD5qNj4P1wxkFtLxCUbY%3D";
        Map<String, String> param = new HashMap<>();
        param.put("projectId", "214");
        String paramStr = "projectId=214";
        FileOutputStream out = new FileOutputStream("D:/test.xls");
        downloadFileByGet(url, null, out);
    }

    public static void doPost(String url, Map<String, String> paramMap) throws IOException {

        List<NameValuePair> paramList = new ArrayList<>();
        paramMap.forEach((key, value) -> {
            paramList.add(new BasicNameValuePair(key, value));
        });
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(paramList));
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {

            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            long length = entity.getContentLength();
            System.out.println(length);
        } catch (Exception e) {

        } finally {
            response.close();
        }
    }

    public static void downloadFileByGet(String url, String param, OutputStream out) {
        if(param != null){
            url = url + "?" + param;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        InputStream in = null;

        try {
            HttpGet httpGet = new HttpGet(url);


            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            in = entity.getContent();

            long length = entity.getContentLength();
            if (length <= 0) {
                System.out.println("下载文件不存在！");
                return;
            }

            System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));


            byte[] buffer = new byte[4096];
            int readLength = 0;
            while ((readLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, readLength);
            }

            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
